package com.yunxin.customui.service.builder.table;

import com.yunxin.tools.commonbean.DataBean;
import com.yunxin.customui.bean.table.TableBean;
import com.yunxin.customui.bean.table.TdBean;
import com.yunxin.customui.bean.table.TrBean;
import com.yunxin.customui.bean.table.datatable.TableBusinessConfig;
import com.yunxin.customui.bean.table.datatable.TableDataTemplate;
import com.yunxin.customui.myinterface.table.ITableBuilder;
import com.yunxin.customui.util.TableUtil;

import java.util.*;

/**
 * 默认的构造器
 *
 * @author LGY
 * @date 2021/09/26
 */
public class DefaultBuilder implements ITableBuilder<Map<String,List<DataBean>>> {

    /**
     * 建立表
     *
     * @param tableBean 表对象
     * @param config    配置
     * @param dataMap   数据
     */
    @Override
    public void buildTable(TableBean tableBean, TableBusinessConfig config, Map<String,List<DataBean>> dataMap) {
        //根据电厂排序，暂时写死，默认排序
        tableBean.setOrderFun(TableUtil.orderFun());

        //处理数据得到要渲染到表格的数据
        List<List<DataBean>> listList;
        if (!config.getMergeType().contains("不合并")){
            listList = TableUtil.handleListDataBeans(config, new ArrayList<>(dataMap.values()));
        }else {
            listList = new ArrayList<>(dataMap.values());
        }
        //创建表格
        buildTable(tableBean,config,listList);
        //最终处理
        TableUtil.handleTableBean(config,tableBean);

    }


    /**
     * 建立表
     * 通过表格配置与获取到的数据转换为TableBean结构
     *
     * @param tableDataConfig  自定义表格配置（存放的表头信息与顺序）
     * @param listListDataBeans 表格获取到的数据（保证数据不漏点 并按时间排序）
     * @param tableBean         表的bean
     * @return {@link TableBean}
     */
    public TableBean buildTable(TableBean tableBean, TableBusinessConfig tableDataConfig, List<List<DataBean>> listListDataBeans) {
        //initTable();
        //行   第一个key id的4-16(电厂位--agc位结束) + 时间  第二个key16-22位，
        // 为了行跟列的点号（businessid）能够匹配上，先生成一个map，让点号对号入座
        Map<String, Map<String, DataBean>> mapMap = new LinkedHashMap<>();
        for (List<DataBean> listListDataBean : listListDataBeans) {
            for (DataBean dataBean : listListDataBean) {
                String dataId = dataBean.getDataId();
                String dataTime = dataBean.getDataTime();
                if (dataTime == null) {
                    dataTime = "";
                }
                Map<String, DataBean> dataMap = new HashMap<>();
                dataMap.put(dataId.substring(16, 22), dataBean);
                Map<String, DataBean> stringDataBeanMap = mapMap.get(dataId.substring(4, 14) + dataTime);
                if (stringDataBeanMap == null) {
                    stringDataBeanMap = new HashMap<>();
                }
                stringDataBeanMap.putAll(dataMap);
                //表格每行数据的数据Id 4-14位必然相等
                mapMap.put(dataId.substring(4, 14) + dataTime, stringDataBeanMap);
            }
        }
        List<TableDataTemplate> tableDataTemplates = tableDataConfig.getTableDataTemplates();
        //TableBean tableBean = new TableBean();
        //第一行表头赋值
        TrBean trBean1 = new TrBean();
        List<TableDataTemplate> businessIds = new ArrayList<>();
        for (TableDataTemplate tableDataTemplate : tableDataTemplates) {
            if (tableDataTemplate.isRealId()) {
                businessIds.add(tableDataTemplate);
                TdBean tdBean = new TdBean();
                tdBean.setValue(tableDataTemplate.getFieldDesc());
                trBean1.getTdBeans().add(tdBean);
            }
        }
        tableBean.getThBeans().add(trBean1);

        int rowCount = 0;
        //mapMap key  id 4-10位+时间
        List<String> keys;
        ArrayList<String> mapMapKeys = new ArrayList<>(mapMap.keySet());
        // 对id进行排序，防止错位
        if (tableBean.getOrderKeyFun() != null) {
            keys = tableBean.getOrderKeyFun().apply(mapMapKeys);
        } else {
            keys = mapMapKeys;
        }
        for (String key : keys) {
            Map<String, DataBean> value = mapMap.get(key);
            TrBean tr = new TrBean();
            tableBean.getTrBeans().add(tr);
            rowCount++;
            for (int j = 0; j < businessIds.size(); j++) {
                TdBean tdBean = new TdBean();
                TableDataTemplate tableDataTemplate = businessIds.get(j);
                DataBean dataBean = value.get(tableDataTemplate.getBusinessId());
                tdBean.setData(tableDataTemplate,dataBean);
                int col = j + 1;
                boolean flag = true;
                for (Integer integer : tableBean.getColOrder()) {
                    if (integer == col) {
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    tableBean.getColOrder().add(col);
                }
                tdBean.setCol(j + 1);
                tableBean.getIdColMapping().putIfAbsent(tableDataTemplate.getBusinessId(),j + 1);
                tdBean.setRow(rowCount);
                tr.getTdBeans().add(tdBean);
            }
        }
        tableBean.setTableId(tableDataConfig.getTableId());
        return tableBean;
    }











}
